//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by 李昊林 on 16/7/18.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 手势识别器
    // 分为7种
#pragma mark - 轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击几次触发方法, 默认为1
    tap.numberOfTapsRequired = 2;
    // 几个手指触发 (按option - alt) 默认为1
    tap.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:tap];
#pragma mark - 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后, 默认0.5s
    longPress.minimumPressDuration = 3;
    // 默认10像素, 长按失败前, 最大的移动距离
    longPress.allowableMovement = 1000;
    [self.view addGestureRecognizer:longPress];
#pragma mark - 轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // direction可以设置多个, 前提多个方向所触发的行为要相同
    // 如果多个方法触发的行为不同, 需要创建多个轻扫的手势
    // 左右 与 上下有冲突
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    // 练习: ImageView添加轻扫换图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 350, 494)];
    imageView.image = [UIImage imageNamed:@"3f84dd575e5444f7b9bd45f74eb52ce8.jpg"];
    [self.view addSubview:imageView];
    // 将图片的交互打开
    imageView.userInteractionEnabled = YES;
    // 按位枚举 4个选项 -> 1 1 1 1
    // 全选的情况 1(3) 1(2) 1(1) 1(0)
    //    UISwipeGestureRecognizer *swipeChangeImageRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeImage:)];
    //    [imageView addGestureRecognizer:swipeChangeImageRight];
    //    swipeChangeImageRight.direction = UISwipeGestureRecognizerDirectionRight;
    //
    //    UISwipeGestureRecognizer *swipeChangeImageLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeImage:)];
    //    [imageView addGestureRecognizer:swipeChangeImageLeft];
    //    swipeChangeImageLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    // 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    
    // 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    // 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screenEdgepan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePan:)];
    screenEdgepan.edges = UIRectEdgeAll;
    [imageView addGestureRecognizer:screenEdgepan];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}

- (void)longPressAction:(UILongPressGestureRecognizer *) longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
    NSLog(@"长按");
}
- (void)swipeAction:(UISwipeGestureRecognizer *) swipe {
    NSLog(@"轻扫");
}

- (void)swipeChangeImage:(UISwipeGestureRecognizer *)swipe {
    if (swipe.direction & UISwipeGestureRecognizerDirectionRight) {
        UIImageView *imageView = (UIImageView *)swipe.view;
        imageView.image = [UIImage imageNamed:@"u=1824248709,3692643173&fm=21&gp=0.jpg"];
        NSLog(@"%ld", swipe.direction);
    }
    if (swipe.direction & UISwipeGestureRecognizerDirectionLeft) {
        UIImageView *imageView = (UIImageView *)swipe.view;
        imageView.image = [UIImage imageNamed:@"3f84dd575e5444f7b9bd45f74eb52ce8.jpg"];
        NSLog(@"%ld", swipe.direction);
    }
}
- (void)panAction:(UIPanGestureRecognizer *)pan {
    CGPoint point = [pan translationInView:pan.view];
    pan.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
    [pan setTranslation:point inView:pan.view];
}
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *) pinch {
    NSLog(@"%f",pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //设置缩放倍数
    pinch.scale = 1;
}
//旋转
- (void)rotationAction:(UIRotationGestureRecognizer *) rotation {
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    //旋转的弧度设置为0
    rotation.rotation = 0;
}
//屏幕边缘
- (void)screenEdgePan:(UIScreenEdgePanGestureRecognizer *) screenEdgePan {
    NSLog(@"屏幕边缘手势");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
